﻿#include "wpns_a2_w.sqf"
#include "wpns_a2_e.sqf"
#include "wpns_oa_w.sqf"
#include "wpns_oa_e.sqf"
#include "wpns_baf_w.sqf"
#include "wpns_pmc.sqf"

// #define __DEBUG__

// liste des armes exclus
_wpns_exclude = [
	"AKS_GOLD",
	"revolver_gold_EP1",

	"Kostey_photos",
	"Kostey_map_case",
	"Kostey_notebook",
	"CDF_dogtags",
	"Moscow_Bombing_File",
	"Cobalt_File",
	"EvPhoto",
	"EvMap",
	"EvMoscow",
	"EvKobalt",
	"EvMoney",
	"EvDogTags",

	"pmc_documents"
];

// Ammunitions to exclude

	//"HandGrenade",
	//"MineE",
	//"IRStrobe",
	//"IR_Strobe_Marker",
_mags_exclude = [
	"HandGrenade_Stone",
	"HandGrenade_East",
	"TimeBomb"
];

_params = _this select 0;
_weaponcoeff = _this select 1;

// Table of all weapons
_alltheweapons = [];
// Table for GPS, etc.
_extraitems = [];

_allwpns = configFile >> "CfgWeapons";
for "_a" from 1 to ((count _allwpns) - 1) do {
	_base = configFile >> "CfgWeapons";
	_wpn = (_base select _a);
	if (getNumber(_wpn >> "scope") == 2) then {
		_slot = getNumber(_wpn >> "type");

		_cfgtbl = toArray (format ["%1",_wpn]);
		_nametbl = [];
		// Elimination de la chaine "BIN\CONFIG.BIN/CFGWEAPONS/"
		for "_s" from 26 to ((count _cfgtbl) - 1) do { _nametbl = _nametbl + [_cfgtbl select _s] };
		_name = toString _nametbl;
		If (_slot < 4096) then {
				_alltheweapons = _alltheweapons + [_name];
		} else {
			// Exculsion des "hard mounted"
			if (_slot != 65536) then {
				_extraitems = _extraitems + [_name];
			};
		};
	};
};

_slots_include = [];
_slots_exclude = [];
_wpnsarray = [];
_excludearray = [];

// Extraction des filtres
If (count _this > 0) then {
	for "_p" from 0 to ((count _params) - 1) do {

		_soloprm  = _params select _p;

		// Filtre selon les slots
		if (typename _soloprm == "SCALAR") then {
			if (_soloprm >= 0) then { _slots_include = _slots_include + [_soloprm] } else { _slots_exclude = _slots_exclude + [abs _soloprm] };
		};

		// Filtre selon les tables prédéfinies (voir includes/répertoires "wpns_arrays\"); remplissage "à l'ancienne"
		if (typename _soloprm == "STRING") then {
			Switch (_soloprm) do {
				case "a2e": {
					_wpnsarray = _wpnsarray + _wpns_a2_e;
				};
				case "a2w": {
					_wpnsarray = _wpnsarray + _wpns_a2_w;
				};
				case "oae": {
					_wpnsarray = _wpnsarray + _wpns_oa_e;
				};
				case "oaw": {
					_wpnsarray = _wpnsarray + _wpns_oa_w;
				};
				case "baf": {
					_wpnsarray = _wpnsarray + _wpns_baf_w;
				};
				case "pmc": {
					_wpnsarray = _wpnsarray + _wpns_pmc;
				};


				case "-a2e": {
					_excludearray = _excludearray + _wpns_a2_e;
				};
				case "-a2w": {
					_excludearray = _excludearray + _wpns_a2_w;
				};
				case "-oae": {
					_excludearray = _excludearray + _wpns_oa_e;
				};
				case "-oaw": {
					_excludearray = _excludearray + _wpns_oa_w;
				};
				case "-baf": {
					_excludearray = _excludearray + _wpns_baf_w;
				};
				case "-pmc": {
					_excludearray = _excludearray + _wpns_pmc;
				};
			};
		};
	};
};


if (count _wpnsarray > 0) then {
	// In this case we already filtered what we wanted
	_alltheweapons = _wpnsarray 
} else {
	// Here are the default values
	_alltheweapons = _alltheweapons + _extraitems;
};
if (count _excludearray > 0) then {
	_alltheweapons = _alltheweapons - _excludearray
};
// Teste l'ajout ou non des armes de jet ou bombes
if ( (20000 in _slots_include) or (not (20000 in _slots_exclude)) ) then {
	_alltheweapons = _alltheweapons - ["put","throw"];
	_alltheweapons = _alltheweapons + ["put","throw"];
	if (count _slots_include > 0) then { _slots_include = _slots_include + [0] };
};
// Retrait des armes "exotiques" ou buggée
_alltheweapons = _alltheweapons - _wpns_exclude;

_fill_crate = {
		private ["_wpns","_coefwpn","_coefmag"];
		_wpns = _this select 0;
		_coefwpn = 0.1 * (_this select 1);
		_coefmag = 2.5 * (_this select 1);
		_inbox_wpns = [];
		_inbox_mags = [];
		_iElCount = 0;
		_wpnArray = [];
		_magArray = [];
#ifdef __DEBUG__		
		diag_log Format["%2 Start Creating Weapon Array: %1 (%3)", count _wpns, diag_tickTime];
#endif		
		for [{_i = 0},{_i < (count _wpns)},{_i = _i + 1}] do {
			_weap = _wpns select _i;
			_slot = getNumber (configFile >> "cfgWeapons" >> _weap >> "type");
#ifdef __DEBUG__		
			diag_log Format["Addding weapon %1 (%2)", _weap, _slot];
#endif		
			_weaponflag = true;
			_putthrowflag = false;
			If (_slot == 0 ) then {
				_weaponflag = false;
				_putthrowflag = true;
			// } else {
				// if ( (not (_slot in _slots_include)) and ((count _slots_include) > 0) ) then { _weaponflag = false };
				// if ( (_slot in _slots_exclude) and ((count _slots_exclude) > 0) ) then { _weaponflag = false };
			};
			if ((_weaponflag) OR (_putthrowflag)) then {

				if ((_slot >= 4096) or (_slot == 0)) then { _slot = 1 };
				if (_slot == 5) then {_slot = 1};
				_nbwpns = round (exp (2.3 / _slot));

				If (_weaponflag) then {
					// Only weapons which are actually weapons need to be added, Put and throw are fake ones.
					if (not (_weap in _inbox_wpns)) then {
						_wpnArray set [count _wpnArray, [1,_weap, ceil (_nbwpns * _coefwpn)]];
						_iElCount = _iElCount + 1;
						_inbox_wpns = _inbox_wpns + [_weap];
					};
				};
				// Ajout des munitions par défaut (= muzzle 0)
				_wpncfg = configFile >> "CfgWeapons" >> _weap;
				_cfgmags = getArray (_wpncfg >> "magazines");
				if (count _cfgmags > 0) then {
					for "_k" from 0 to ((count _cfgmags) - 1) do {
						_mag = _cfgmags select _k;
						if (not (_mag in _inbox_mags)) then {
							_magArray set [count _magArray, [2,_mag, round (_nbwpns * _coefmag)]];
							_iElCount = _iElCount + 1;
							_inbox_mags = _inbox_mags + [_mag];
						};
						//sleep 0.001;
					};
				};

				// Ajout des munitions secondaire (= muzzle 1+)
				_wpnmuzzles = getArray (_wpncfg >> "muzzles");
				// _wpnmuzzles > 1 car le 1er est systèmatiquement le muzzle de l'arme principale
				if (count _wpnmuzzles > 1) then {
					for "_m" from 1 to ((count _wpnmuzzles) - 1) do {
						_muzzlmags = getArray(configFile >> "cfgWeapons" >> _weap >> (_wpnmuzzles select 1) >> "magazines");
						for "_p" from 0 to ((count _muzzlmags) - 1) do {
							_mag = _muzzlmags select _p;
							if (not (_mag in _inbox_mags)) then {
								_magArray set [count _magArray, [2,_mag, round (_nbwpns * _coefmag)]];
								_iElCount = _iElCount + 1;
								_inbox_mags = _inbox_mags + [_mag];
							};
							//sleep 0.001;
						};
					};
				};

			};
			sleep 0.001;
		};
#ifdef __DEBUG__		
		diag_log Format["%2 Finished Creating Weapon Array: %1 (%3)", _iElCount, diag_tickTime];
#endif		
		_wpnArray = _wpnArray + _magArray;
		_wpnArray;
	};

#ifdef __DEBUG__		
diag_log Format ["Weapon Classes: %1",_alltheweapons];
#endif		
_weaponArray = [_alltheweapons,_weaponcoeff] call _fill_crate;
#ifdef __DEBUG__		
diag_log Format ["Weapon Array: %1",_weaponArray];
#endif		

_weaponArray;
